<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>获取一系列外部变量，并且可以通过过滤器处理它们</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.filter-id.html">« filter_id</a></li>
      <li style="float: right;"><a href="function.filter-input.html">filter_input »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.filter.html">过滤器函数</a></li>
    <li>获取一系列外部变量，并且可以通过过滤器处理它们</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.filter-input-array" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">filter_input_array</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.2.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">filter_input_array</span> &mdash; <span class="dc-title">获取一系列外部变量，并且可以通过过滤器处理它们</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.filter-input-array-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>filter_input_array</strong></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$type</code></span>, <span class="methodparam"><span class="type"><span class="type">array</span>|<span class="type">int</span></span> <code class="parameter">$options</code><span class="initializer"> = <strong><code>FILTER_DEFAULT</code></strong></span></span>, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$add_empty</code><span class="initializer"> = <strong><code>true</code></strong></span></span>): <span class="type"><span class="type">array</span>|<span class="type"><span class="type false">false</span></span>|<span class="type">null</span></span></div>

  <p class="para rdfs-comment">
   这个函数当需要获取很多变量却不想重复调用<span class="function"><a href="function.filter-input.html" class="function">filter_input()</a></span>时很有用。
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.filter-input-array-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">type</code></dt>

     <dd>

      <p class="para">
       <strong><code>INPUT_GET</code></strong>, <strong><code>INPUT_POST</code></strong>,
       <strong><code>INPUT_COOKIE</code></strong>, <strong><code>INPUT_SERVER</code></strong>, or
       <strong><code>INPUT_ENV</code></strong>之一。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">options</code></dt>

     <dd>

      <p class="para">
       一个定义参数的数组。一个有效的键必须是一个包含变量名的<span class="type">string</span>，一个有效的值要么是一个<a href="filter.filters.html" class="link">filter type</a>，或者是一个<span class="type">array</span>
       指明了过滤器、标示和选项。如果值是一个数组，那么它的有效的键可以是 <code class="literal">filter</code>，
       用于指明 <a href="filter.filters.html" class="link">filter type</a>，<code class="literal">flags</code> 
       用于指明任何想要用于过滤器的标示，或者 <code class="literal">options</code> 用于指明任何想要用于过滤器的选项。
       参考下面的例子来更好的理解这段说明。
      </p>
      <p class="para">
       这个参数也可以是一个<a href="filter.constants.html" class="link">filter constant</a>的整数。那么数组中的所有变量都会被这个过滤器所过滤。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">add_empty</code></dt>

     <dd>

      <p class="para">
       在返回值中添加 <strong><code>null</code></strong> 作为不存在的键。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.filter-input-array-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时包含请求变量值的数组。如果通过 <code class="parameter">type</code> 指定的输入数组没有值可以填充，当没有指定 
   <strong><code>FILTER_NULL_ON_FAILURE</code></strong> flag 时函数返回 <strong><code>null</code></strong>，否则为 <strong><code>false</code></strong>。其它失败返回 <strong><code>false</code></strong>。
  </p>
  <p class="para">
   如果过滤失败，数组值将为 <strong><code>false</code></strong>，如果变量未设置，返回 <strong><code>null</code></strong>。如果使用 <strong><code>FILTER_NULL_ON_FAILURE</code></strong>
   flag，变量未设置将返回 <strong><code>false</code></strong>，过滤失败将返回 <strong><code>null</code></strong>。如果 <code class="parameter">add_empty</code> 参数为
   <strong><code>false</code></strong>，不会为复位的变量添加数组元素。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.filter-input-array-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5064">
    <p><strong>示例 #1 <span class="function"><strong>filter_input_array()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">/*&nbsp;data&nbsp;actually&nbsp;came&nbsp;from&nbsp;POST<br />$_POST&nbsp;=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;'product_id'&nbsp;=&gt;&nbsp;'libgd&lt;script&gt;',<br />&nbsp;&nbsp;&nbsp;&nbsp;'component'&nbsp;&nbsp;=&gt;&nbsp;array('10'),<br />&nbsp;&nbsp;&nbsp;&nbsp;'version'&nbsp;&nbsp;&nbsp;&nbsp;=&gt;&nbsp;'2.0.33',<br />&nbsp;&nbsp;&nbsp;&nbsp;'testarray'&nbsp;&nbsp;=&gt;&nbsp;array('2',&nbsp;'23',&nbsp;'10',&nbsp;'12'),<br />&nbsp;&nbsp;&nbsp;&nbsp;'testscalar'&nbsp;=&gt;&nbsp;'2',<br />);<br />*/<br /><br /></span><span style="color: #0000BB">$args&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'product_id'&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">FILTER_SANITIZE_ENCODED</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'component'&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">'filter'&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">FILTER_VALIDATE_INT</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'flags'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">FILTER_REQUIRE_ARRAY</span><span style="color: #007700">,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'options'&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">'min_range'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'max_range'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'version'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">FILTER_SANITIZE_ENCODED</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'doesnotexist'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">FILTER_VALIDATE_INT</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'testscalar'&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'filter'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">FILTER_VALIDATE_INT</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'flags'&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">FILTER_REQUIRE_SCALAR</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'testarray'&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'filter'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">FILTER_VALIDATE_INT</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'flags'&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">FILTER_REQUIRE_ARRAY</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br /><br />);<br /><br /></span><span style="color: #0000BB">$myinputs&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">filter_input_array</span><span style="color: #007700">(</span><span style="color: #0000BB">INPUT_POST</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$args</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$myinputs</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(6) {
  [&quot;product_id&quot;]=&gt;
  string(17) &quot;libgd%3Cscript%3E&quot;
  [&quot;component&quot;]=&gt;
  array(1) {
    [0]=&gt;
    int(10)
  }
  [&quot;version&quot;]=&gt;
  string(6) &quot;2.0.33&quot;
  [&quot;doesnotexist&quot;]=&gt;
  NULL
  [&quot;testscalar&quot;]=&gt;
  int(2)
  [&quot;testarray&quot;]=&gt;
  array(4) {
    [0]=&gt;
    int(2)
    [1]=&gt;
    int(23)
    [2]=&gt;
    int(10)
    [3]=&gt;
    int(12)
  }
}
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.filter-input-array-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    在 <strong><code>INPUT_SERVER</code></strong> 数组中并没有 <code class="literal">REQUEST_TIME</code> ，因为它是被稍后插入到 <var class="varname"><a href="reserved.variables.server.html" class="classname">$_SERVER</a></var> 中的。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.filter-input-array-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.filter-input.html" class="function" rel="rdfs-seeAlso">filter_input()</a> - 通过名称获取特定的外部变量，并且可以通过过滤器处理它</span></li>
    <li class="member"><span class="function"><a href="function.filter-var-array.html" class="function" rel="rdfs-seeAlso">filter_var_array()</a> - 获取多个变量并且过滤它们</span></li>
    <li class="member"><a href="filter.filters.html" class="xref">Types of filters</a></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>